package com.mollytask.institution;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.mollytask.R;
import com.mollytask.bean.DataFill;
import com.mollytask.bean.Institution;
import com.mollytask.bean.MyUser;
import com.weigan.loopview.LoopView;
import com.weigan.loopview.OnItemSelectedListener;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * Created by freshxu on 2017/5/23.
 */

public class DataFillFragment extends Fragment {

    public static DataFillFragment newInstance() {
        DataFillFragment myFragment = new DataFillFragment();
        return myFragment;
    }


    private LoopView mYearloop;
    private LoopView mQuartLoop;
    private Button mFillBtn;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1000:
                    Toast.makeText(getActivity(), "该时间数据已经填写", Toast.LENGTH_LONG).show();
                    break;
                case 2000:
                    Intent intent = new Intent(getActivity(), DataFillActivity.class);
                    intent.putExtra("year", msg.arg1);
                    intent.putExtra("quart", msg.arg2);
                    startActivity(intent);
                    break;
            }
        }
    };


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.framgent_datafill, container, false);
        mYearloop = (LoopView) view.findViewById(R.id.loopView1);
        mQuartLoop = (LoopView) view.findViewById(R.id.loopView2);
        mFillBtn = (Button) view.findViewById(R.id.btn_datadill);

        initInstitute();

        return view;
    }


    //初始化 公共机构页面
    private void initInstitute() {
        List<String> list = new ArrayList<>();
        for (int i = 2017; i >= 2000; i--) {
            list.add(i + "年");
        }
        mYearloop.setItems(list);
        List<String> list2 = new ArrayList<>();
        for (int i = 1; i <= 2; i++) {
            list2.add("第" + i + "季度");
        }
        mQuartLoop.setItems(list2);

        mYearloop.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                if (index != 0) {
                    list2.clear();
                    for (int i = 1; i <= 4; i++) {
                        list2.add("第" + i + "季度");
                    }
                } else {
                    mQuartLoop.setCurrentPosition(0);
                    list2.clear();
                    for (int i = 1; i <= 2; i++) {
                        list2.add("第" + i + "季度");
                    }
                }
            }
        });

        setListener();

    }

    private void setListener() {
        //点击填报按钮
        mFillBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int year = 2017 - mYearloop.getSelectedItem();
                int quart = 1 + mQuartLoop.getSelectedItem();

                //得到当前用户
                MyUser myUser = BmobUser.getCurrentUser(MyUser.class);
                //查村数据库存储的信息
                BmobQuery<Institution> query = new BmobQuery<>();
                query.addWhereEqualTo("mInCode", myUser.getCode());

                query.findObjects(new FindListener<Institution>() {
                    @Override
                    public void done(List<Institution> list, BmobException e) {

                        if (e == null) {
                            Institution mInstitution = list.get(0);

                            //查询该季度是否已经上报过
                            BmobQuery<DataFill> bmobQuery = new BmobQuery<DataFill>();
                            bmobQuery.addWhereEqualTo("mFillId", mInstitution.getmInCode() + year + quart);
                            bmobQuery.findObjects(new FindListener<DataFill>() {
                                @Override
                                public void done(List<DataFill> list, BmobException e) {

                                    if (e == null) {
                                        if (list.size() >= 1) {
                                            mHandler.sendEmptyMessage(1000);
                                        } else {
                                            Message message = Message.obtain();
                                            message.arg1 = year;
                                            message.arg2 = quart;
                                            message.what = 2000;
                                            mHandler.sendMessage(message);
                                        }
                                    } else if (e.getErrorCode() == 101) {
                                        Message message = Message.obtain();
                                        message.arg1 = year;
                                        message.arg2 = quart;
                                        message.what = 2000;
                                        mHandler.sendMessage(message);
                                    }
                                }
                            });

                        }
                    }
                });

            }
        });
    }

}
